<?php

/**
 * This is the model class for table "zy_event_field".
 *
 * The followings are the available columns in table 'zy_event_field':
	 * @property integer $field_id
	 * @property string $field_label
	 * @property string $field_name
	 * @property string $field_type
	 * @property string $field_option
 */
class EventField extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return EventField the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_event_field';
	}
	public function primaryKey()
	{
		return 'field_id';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('field_label_cn,field_label_en, field_name, field_type, remark,field_option_cn,field_option_en', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('field_id, field_label_cn,field_label_en, field_name, field_type, field_option_cn,field_option_en,event_id,user_id,is_delete, remark, create_time, create_user, update_time, update_user', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'field_id' =>Yii::t("EventField","Field ID"),
			'field_label_cn' =>Yii::t("EventField","Field Label Cn"),
			'field_label_en' =>Yii::t("EventField","Field Label En"),
			'field_name' =>Yii::t("EventField","Field Name"),
			'field_type' =>Yii::t("EventField","Field Type"),
			'field_option_cn' =>Yii::t("EventField","Field Option Cn"),
			'field_option_en' =>Yii::t("EventField","Field Option En"),
			'event_id' =>Yii::t("EventField","Event ID"),
			'user_id' =>Yii::t("EventField","User ID"),
			'is_delete' =>Yii::t("EventField","Is Delete"),
			'remark' =>Yii::t("EventField","Remark"),
			'create_time' =>Yii::t("EventField","Create Time"),
			'create_user' =>Yii::t("EventField","Create User"),
			'update_time' =>Yii::t("EventField","Update Time"),
			'update_user' =>Yii::t("EventField","Update User"),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('field_id',$this->field_id);
		$criteria->compare('field_label_cn',$this->field_label_cn,true);
		$criteria->compare('field_label_en',$this->field_label_en,true);
		$criteria->compare('field_name',$this->field_name,true);
		$criteria->compare('field_type',$this->field_type,true);
		$criteria->compare('field_option_cn',$this->field_option_cn,true);
		$criteria->compare('field_option_en',$this->field_option_en,true);
		$criteria->compare('event_id',$this->event_id,true);
		if(Yii::app()->user->level() == 1){
			$criteria->compare('user_id',$this->user_id,true);
		}
		if(Yii::app()->user->level() == 9){
			$criteria->compare('user_id',Yii::app()->user->id,true);
		}
		
		$criteria->compare('is_delete',$this->is_delete);
		$criteria->compare('remark',$this->remark,true);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('create_user',$this->create_user,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('update_user',$this->update_user,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(  
           		 'defaultOrder'=>'field_id DESC',  
       		 ),  
       		'pagination'=>array(  
           		 'pageSize'=>20  
       		 ),  
		));
	}
	
		//通过字段的id得到它的名字
	public function getEventfieldName($event_field)
	{
//		$eventfieldlist=array();
//		$arr = str_replace("|", ",", $event_field);	
//		$sql="select * from zy_event_field where field_id in ($arr)";
//		$result=Yii::app()->db->createCommand($sql)->queryAll();
//		for($i=0;$i<count($result);$i++)
//		{
//		$eventfieldlist[$i]= $result[$i]['field_label_cn'];
//		}
//		$eventfieldName=implode('|',$eventfieldlist);
		$eventfieldName=unserialize($event_field);
		return $eventfieldName;
	}
	//查找指定的活动id和id为0的字段
	public function getField($event_id)
	{
		$cd=new CDbCriteria;//CDbCriteria数据库查询	
		$cd->addInCondition("event_id", array(0,$event_id));
		$model=EventField::model()->findAll($cd);
		return $model;
	}
	
	//找到该用户名下的活动
	public function getEvent()
	{
		$cd=new CDbCriteria;//CDbCriteria数据库查询	
		$cd->compare("user_id", Yii::app()->user->id);
		$model=Event::model()->findAll($cd);
		return CHtml::listData( $model, 'event_id', 'event_title');
	}
}